package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.R.integer;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.dto.MyOrder;
import com.hmyd.hmyd_android.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;

public class MyOrderAdapter extends BaseAdapter {
	private Context mContext;
	private List<MyOrder> mDatas;
	private LayoutInflater mInflater;
	private LinearLayout.LayoutParams params;
	private ViewGroup parent;
	private ListView mListView;
	OnConfirmListener callBck;

	public interface OnConfirmListener {
		public void onConfirm(String id, int postion,String state);

		public void onDetail(String Id,String state);
	}

	public MyOrderAdapter(Context context, List<MyOrder> lists) {
		this.mContext = context;
		this.mDatas = lists;
		mInflater = LayoutInflater.from(context);
		callBck = (OnConfirmListener) context;

		params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		params.setMargins(10, 10, 10, 10);
		params.height = 230;
		params.width = 230;
	}

	@Override
	public int getCount() {
		return mDatas != null ? mDatas.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (mListView == null) {
			mListView = (ListView) parent;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_my_order_item, null);
			holder = new ViewHolder();
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_order_num = (TextView) convertView.findViewById(R.id.order_num);
			holder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_total_points = (TextView) convertView.findViewById(R.id.total_points);
			holder.btn_confirm = (Button) convertView.findViewById(R.id.btn_confirm);
			holder.mGallery = (LinearLayout) convertView.findViewById(R.id.id_gallery);
			holder.btn_detail = (Button) convertView.findViewById(R.id.btn_detail);
			holder.hScrollView = (HorizontalScrollView) convertView.findViewById(R.id.hs);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final MyOrder item = mDatas.get(position);

		holder.tv_order_num.setText(item.id);
		holder.tv_name.setText(item.addressee);
		holder.tv_time.setText(Utils.getDateString(item.createdTime * 1000)+"");

		// state：Processing(处理中)、Delivery（发货中）、Treat（待处理）、Completed（已完成）、Return（退换货）
		if (item.state.equals("Processing")) {
			holder.tv_state.setText("处理中");
			holder.btn_confirm.setVisibility(View.VISIBLE);
			holder.btn_confirm.setEnabled(false);
			holder.btn_confirm.setBackgroundResource(R.drawable.confirm_receive_unactive);
		} else if (item.state.equals("Delivery")) {
			holder.tv_state.setText("发货中");
			holder.btn_confirm.setVisibility(View.VISIBLE);
			holder.btn_confirm.setEnabled(true);
		} else if (item.state.equals("Treat")) {
			holder.tv_state.setText("待处理");
			holder.btn_confirm.setVisibility(View.VISIBLE);
			holder.btn_confirm.setEnabled(false);
			holder.btn_confirm.setBackgroundResource(R.drawable.confirm_receive_unactive);
		} else if (item.state.equals("Completed")) {
			holder.tv_state.setText("已完成");
			holder.btn_confirm.setVisibility(View.GONE);
		} else if (item.state.equals("Return")) {
			holder.tv_state.setText("退换货");
			holder.btn_confirm.setVisibility(View.GONE);
		} else {
			holder.tv_state.setText("处理中");
			holder.btn_confirm.setVisibility(View.VISIBLE);
			holder.btn_confirm.setEnabled(false);
			holder.btn_confirm.setBackgroundResource(R.drawable.confirm_receive_unactive);
		}

		int total = 0;
		holder.mGallery.removeAllViews();
		ImageView img;
		int size = item.records.size();
		for (int i = 0; i < size; i++) {
			total = total + item.records.get(i).presentPrice * item.records.get(i).number;
			img = new ImageView(mContext);
			img.setBackgroundResource(R.drawable.shop_goods_imageview_bg);
			img.setTag(item.records.get(i).icon);
			img.setScaleType(ScaleType.FIT_XY);
			holder.mGallery.setTag(item.records.get(i).icon);
			holder.hScrollView.setTag(item.records.get(i).icon);
			if (img.getTag() != null && img.getTag().equals(item.records.get(i).icon)) {
				AppBasic.getBitmapUtils(mContext).display(img, item.records.get(i).icon);
			}

			holder.mGallery.addView(img, params);
		}
		holder.tv_total_points.setText(total + "");
		holder.btn_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callBck.onConfirm(item.id, position,item.state);
			}
		});

		holder.btn_detail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callBck.onDetail(item.id,item.state);
			}
		});

		return convertView;
	}

	class Task extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			return null;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

	class ViewHolder {

		private Button btn_detail;
		private Button btn_confirm;
		private LinearLayout mGallery;

		private TextView tv_name;
		private TextView tv_order_num;
		private TextView tv_state;
		private TextView tv_time;
		private TextView tv_total_points;
		private HorizontalScrollView hScrollView;

	}

}
